import { addArea,downArea } from './riverWater'
import { flatOldGate,removeWaterSystem,createFlood,openGate,closeGate} from './addFloodgate'


export default class Effect5 {
  constructor(viewer){
    this.viewer = viewer
    // this.tile3d = null
    // this.tile3d1 = null
    this.waterEffect = null

    this.highEntity = null
    this.highValue = 33
    this.lowEntity = null
    this.lowValue = 30

    this.yanmoEntity = null

    this.shuiweiTimer = null
    this.xiangjiangTimer = null
    
    this.start()
  }

  start(){
    // this.addModel()
    //加入河流水面渲染

    // let gui = new dat.GUI();
    // addRiverWater(this.viewer);

    //跳到指定位置,开启淹没
    this.viewer.camera.flyTo({
      destination: Cesium.Cartesian3.fromDegrees(113.14732861806448,27.86085148357273,1031.2835221580895), //设置位置
      orientation: {
        heading: Cesium.Math.toRadians(320.76672066308583),
        pitch: Cesium.Math.toRadians(-46.837093193769235),
        roll: 0
      },
      duration:4
    });

    //4s后执行河道淹没旁边
    setTimeout(() => {
      this.start5Ather()
    }, 4000);
    

    this.highEntity = this.viewer.entities.add({
      name:'1',
      position:Cesium.Cartesian3.fromDegrees(113.12425753259735,27.854658401763434,this.highValue+5),
      label:{
        text: `${this.highValue}M`,
        font:'22px sans-serif',
        fillColor:Cesium.Color.ORCHID
      }
    })

    this.lowEntity = this.viewer.entities.add({
      name:'2',
      position:Cesium.Cartesian3.fromDegrees(113.12412411899265,27.853921627676886,this.lowValue+7),
      label:{
        text:  `${this.lowValue}M`,
        font:'22px sans-serif',
        fillColor:Cesium.Color.WHITE
      }
    })

    this.yanmoEntity = this.viewer.entities.add({
      name:'2',
      position:Cesium.Cartesian3.fromDegrees(113.14101075917627,27.868395833553095,38),
      label:{
        text:  ``,
        font:'25px sans-serif',
        fillColor:Cesium.Color.WHITE
      }
    })
    
    this.shuiweiTimer = setInterval(() => {
      // 1是加 2是减
      this.updatePosition(1)
    }, 2500);
    
  }

   updatePosition(type,callback = null){
    if(this.highEntity){
      if(type === 1){
        this.highValue++
      } else {
        this.highValue--
      }
      if(this.highValue === 45 || this.highValue === 31){
        clearInterval(this.shuiweiTimer)
        if(this.xiangjiangTimer){
          clearInterval(this.xiangjiangTimer)
        }
        if(this.highValue === 31){
          setTimeout(() => {
            closeGate(this.viewer,[4,5,6,7])
            removeWaterSystem(this.viewer)
            if(callback){
              callback()
            }
          }, 1000);
        }
      }
      
      this.highEntity.position = Cesium.Cartesian3.fromDegrees(113.12425753259735,27.854658401763434,this.type === 1 ? this.highValue :43)
      this.highEntity.label._text._value = `${this.highValue}M`
    }
  }

  start5Ather(){
    this.waterEffect&&this.waterEffect.destroy()
    this.waterEffect = null

    const drawInterval = 5
    this.waterEffect = new Cesium.WaterFloodSimulation(this.viewer,{showFlowField:true});
    this.waterEffect.loadFile(
      {
          Grid: "data/白石港/gridData_baishigang.json",
          Info: {
              Depth: "data/白石港/depth_baishigang.csv",
              Stage: "data/白石港/stage_baishigang.csv",
              U: "data/白石港/u_baishigang.csv",
              V: "data/白石港/v_baishigang.csv",
          }
      },
      {
          colorTable3:'img/colorTable4.png',
          minCameraHeight:300,
          rectPositions:[111.693258,28.800846,114.912632,28.829140,114.508624,26.865031,111.814421,26.924976,111.693258,28.800846],
          gridIdIndex:0,
          gridId:'单元编号',
          showFlowField: true,
          flowLineHeight:50,
          // 水面反射绘制
          reflectPlaneHeight: 1,
          reflectPlaneCenter: true,
          renderMode: 1,
          renderPass: [2],
          // 水面折射绘制 修改为copy当前帧的颜色纹理 提升性能
          renderMode2: 1,
          renderPass2: [],

          // 绘制类型 1 水面  2 水体
          drawType: 1,
          // 默认绘制批次 默认值 0 第一批
          drawIndex: 0, // 默认绘制第100批数据
          // 绘制批次的间隔 数据为多少分钟一批 这里数据5分钟/1批 内部会做颜色插值和透明过渡 更平滑
          drawInterval: drawInterval,
          // drawColors: "./data/colorTable2.png",
          // drawColors2: "./data/colorTable.png",

          // 水深的min和max阈值 用于渲染颜色
          maxHeight: 3,
          minHeight: 0,

          // gpu纹理大小 默认支持512*512=>262144网格数据 不够可以增加到1024 最大4096 需要考虑机器web的上限和实际性能
          texWidth: 512,
          texHeight: 512,
          // 水面高度 默认值-1 渲染当前批次数据最后一组的水位高度 避免重复绘制影响性能
          waterHeight: 30,
          // 渲染水位顶点动画 大场景慎用 0 不渲染属性高度  1 渲染属性高度 2 两倍渲染属性高度 以此类推
          renderHeight: 0,
          // 水面偏移高度 默认值为 0  在原始高度上偏移
          offsetHeight: 0,
          // 无效值过滤 默认值为0  当前网格水深为0时渲染透明
          isAlpha: -1,

          // 透明插值 前后两批数据通过播放进行透明度插值 增加推演多动态 批次之间也记录了前后两批数据做颜色插值 效果更平滑
          alphaLerp: true,

          startTime: Cesium.JulianDate.fromIso8601("2024-07-29T00:30Z"),
          stopTime: Cesium.JulianDate.fromIso8601("2024-08-04T23:00Z"),

      });

    this.waterEffect.loadEndEvent.addEventListener(() => {
    
        // 配置当前水面
        this.waterEffect.appearance.material.uniforms.u_baseColorType = 0.5; // 两种水面混合
        this.waterEffect.appearance.material.uniforms.oceanOneOverAmplitudeLowAltitude = 0.41; // 浪高
        this.waterEffect.appearance.material.uniforms.oceanFrequencyLowAltitude = 1500; // 密度
        this.waterEffect.appearance.material.uniforms.u_waterSpeed = 200; // 水流速
        this.waterEffect.appearance.material.uniforms.u_causticIntersectionFade = 3.25;  // 焦散缩放
        this.waterEffect.appearance.material.uniforms.u_causticScale = 0.17;  // 焦散缩放
        this.waterEffect.appearance.material.uniforms.u_FoamScale = 2.29; // 白浪缩放
        this.waterEffect.appearance.material.uniforms.u_FoamIntersectionFade = 1.96; // 白浪强度
        this.waterEffect.appearance.material.uniforms.u_WaterDeepColorAlpha = 0.65; // 深水透明度

        this.waterEffect.appearance.material.uniforms.luminance = 0.4;
        this.waterEffect.appearance.material.uniforms.baseColor = Cesium.Color.fromCssColorString('#c18e2e');

        this.waterEffect.showFlowField = false;
        this.waterEffect.isAlpha = 0.0;
        // this.waterEffect.alphaLerp = true;
        // this.waterEffect._waterPrimitive.show = false;
    })

      //监听每个更新批次
      this.waterEffect.palyEvent.addEventListener((batchIndex, minutes) => {
        this.yanmoEntity.label._text._value = `${Math.floor(minutes / 60)}小时`
        if(batchIndex == 83){
          // this.waterEffect.pause();
        }
    })

    setTimeout(() => {
      this.waterEffect.appearance.material.uniforms.u_baseColorType = 0.5
      this.waterEffect.alpha = 1
      this.waterEffect.maxHeight = 8
      this.waterEffect.minHeight = 0
      this.waterEffect.flowLineSpeed = 0.01
            
      this.waterEffect.isAlpha = 0
      
      this.waterEffect.forward(50)
    }, 2000);
    
  }

  start6(callback){
   
    //跳到闸口 开闸  降水位
      this.viewer.camera.flyTo({
        destination: Cesium.Cartesian3.fromDegrees(113.12703253869023,27.852580269019892,271.2529446731685), //设置位置
        orientation: {
          heading: Cesium.Math.toRadians(318.8777848703561),
          pitch: Cesium.Math.toRadians(-32.32708643395086),
          roll: 0
        },
      });

      setTimeout(() => {
        //关闭水闸
      closeGate(this.viewer,[0,1,2,3])
      }, 2000);
      setTimeout(() => {
        //打开水泵
      openGate(this.viewer,[4,5,6,7])
      }, 3000);

      createFlood(this.viewer);

      // downArea()
      clearInterval(this.shuiweiTimer)
      this.shuiweiTimer = setInterval(() => {
        // 1是加 2是减
        this.updatePosition(2,callback)
      }, 2500);
      
      //湘江水位增加 
      this.xiangjiangTimer = setInterval(()=>{
        this.lowValue++
        this.lowEntity.label._text._value = `${this.lowValue}M`
      },7500)

  }

  start7(){
    this.viewer.camera.flyTo({
      destination: Cesium.Cartesian3.fromDegrees(113.14732861806448,27.86085148357273,1031.2835221580895), //设置位置
      orientation: {
        heading: Cesium.Math.toRadians(320.76672066308583),
        pitch: Cesium.Math.toRadians(-46.837093193769235),
        roll: 0
      },
      duration:1.5,
      complete:()=>{
        this.waterEffect.cancelPause()
      }
    });
  }

  clear(){
    
  }

}